<?php
declare (strict_types = 1);

namespace app\type\controller;

use app\type\model\Type;
use app\type\validate\User;
use think\facade\View;
use think\exception\ValidateException;

class Blog
{
    //
    public function form()
    {
        return View::fetch('index/form');
    }

    public function add()
    {
        $data = input();
        try {
            validate(User::class)->check($data);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            dump($e->getError());
        }

        $name = rand(1,9999999);
        $file = request()->file('img');
//        $savename = \think\facade\Filesystem::putFile( 'topic', $file);
        $image = \think\Image::open("$file");
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
        $image->thumb(200,200,\think\Image::THUMB_CENTER)->water('./logo.png')->save('./storage/topic/20211216/'."$name".'png');
        $obj = new Type();
        $res = $obj->add($data);
        if ($res)
        {
            return redirect('list');
        }
    }

    public function list()
    {
        $obj = new Type();
        $list = $obj->list();
        if ($list)
        {
            return View::fetch('index/list',compact('list'));
        }
    }
}
